/**
 * Copyright 2008 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kiff.action.install;

import kiff.action.BaseAction;
import kiff.entity.Setting;
import kiff.entity.User;
import kiff.service.InstallService;

import org.apache.struts2.interceptor.validation.SkipValidation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Inject;

import com.opensymphony.xwork2.validator.annotations.EmailValidator;
import com.opensymphony.xwork2.validator.annotations.FieldExpressionValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;

/**
 * Installation Action.
 * @author Adam Ruggles
 * @version $Id: InstallAction.java 63 2008-11-03 07:20:48Z a.ruggles $
 */
public class InstallAction extends BaseAction {
	/**
	 * Serial Version UID.
	 */
	private static final long serialVersionUID = 8630990417426235270L;

    /**
	 * The Installation Service, contains the business logic for the installation routines.
	 */
	private InstallService installService;

	/**
     * The <code>Logger</code> is used by the application to generate a log messages.
     */
    private final Logger logger = LoggerFactory.getLogger(InstallAction.class);

	/**
	 * Properties used to set the board details.
	 */
	private String siteName, siteDescription;

	/**
	 * Properties used to create the administrative user account.
	 */
	private String username, email, password, password2;
	
	/**
	 * Default method overrides the super class to skip validation.
	 * @return SUCCESS.
	 */
	@Override @SkipValidation
	public String execute() {
		logger.debug("Executing default method");
		return SUCCESS;
	}

	/**
	 * Returns email.
	 * @return the email.
	 */
	@RequiredStringValidator(message = "Required Field.", key = "validation.required")
	@EmailValidator(message = "Enter a valid email.", key = "validation.email")
	public String getEmail() {
		return email;
	}

	/**
	 * Returns password.
	 * @return the password.
	 */
	@RequiredStringValidator(message = "Required Field.", key = "validation.required")
	@StringLengthFieldValidator(trim = true, minLength = "3" , maxLength = "16",
			message = "Must be between ${minLength} and ${maxLength} characters.", key = "validation.length")
	public String getPassword() {
		return password;
	}

	/**
	 * Returns password2.
	 * @return the password2.
	 */
	@FieldExpressionValidator(expression = "password2.equals(password)",
			message = "Passwords do not match.", key = "validation.match.passwords")
	@RequiredStringValidator(message = "Required Field.", key = "validation.required")
	public String getPassword2() {
		return password2;
	}

	/**
	 * Returns siteDescription.
	 * @return the siteDescription.
	 */
	@RequiredStringValidator(message = "Required Field.", key = "validation.required")
	public String getSiteDescription() {
		return siteDescription;
	}

	/**
	 * Returns siteName.
	 * @return the siteName.
	 */
	@RequiredStringValidator(message = "Required Field.", key = "validation.required")
	public String getSiteName() {
		return siteName;
	}

	/**
	 * Returns username.
	 * @return the username.
	 */
	@RequiredStringValidator(message = "Required Field.", key = "validation.required")
	@StringLengthFieldValidator(trim = true, minLength = "3" , maxLength = "16",
			message = "Must be between ${minLength} and ${maxLength} characters.", key = "validation.length")
	public String getUsername() {
		return username;
	}

	/**
	 * Saves the installation settings.
	 * @return SUCCESS if the installation was successful.
	 */
	public String save() {
		User administrator = new User();
		administrator.setUsername(username);
		administrator.setEmail(email);
		administrator.setPassword(password);
		administrator.setActive(true);
		installService.install(administrator, Setting.SITE_NAME, siteName, Setting.SITE_DESCRIPTION, siteDescription);
		return SUCCESS;
	}

	/**
	 * Sets email.
	 * @param email the email to set.
	 */
	public void setEmail(final String email) {
		this.email = email;
	}

	/**
	 * Sets the installService.
	 * @param installService The installService to set.
	 */
	@Inject
	public void setInstallService(final InstallService installService) {
		this.installService = installService;
	}

	/**
	 * Sets password.
	 * @param password the password to set.
	 */
	public void setPassword(final String password) {
		this.password = password;
	}

	/**
	 * Sets password2.
	 * @param password2 the password2 to set.
	 */
	public void setPassword2(final String password2) {
		this.password2 = password2;
	}

	/**
	 * Sets siteDescription.
	 * @param siteDescription the siteDescription to set.
	 */
	public void setSiteDescription(final String siteDescription) {
		this.siteDescription = siteDescription;
	}

	/**
	 * Sets siteName.
	 * @param siteName the siteName to set.
	 */
	public void setSiteName(final String siteName) {
		this.siteName = siteName;
	}

	/**
	 * Sets username.
	 * @param username the username to set.
	 */
	public void setUsername(final String username) {
		this.username = username;
	}
}
